﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExternalConnections.CApECExcelManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using Microsoft.Office.Interop.Excel;
using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;


namespace DnProcessSimulateCommands.ExternalConnections;

internal class CApECExcelManager
{
  protected Application m_excelApp;
  protected Workbook m_workbook;
  protected Worksheet m_sheet;
  private bool m_cultureInfoChanged;
  private CultureInfo m_oldCultureInfo;

  internal CApECExcelManager()
  {
  }

  internal virtual bool Start()
  {
    bool flag = false;
    try
    {
      if (this.m_excelApp == null)
      {
        if (TxAssemblyInfoEx.IsExcelInstalled(false, ""))
        {
          this.SetThreadCultureInfo(new CultureInfo("en-US"));
          this.m_excelApp = (Application) Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
          flag = this.m_excelApp != null;
        }
      }
    }
    catch (COMException ex)
    {
      int errorCode = ex.ErrorCode;
      flag = false;
      this.ResetThreadCultureInfo();
    }
    return flag;
  }

  internal virtual void Release()
  {
    if (this.m_sheet != null)
    {
      Marshal.ReleaseComObject((object) this.m_sheet);
      this.m_sheet = (Worksheet) null;
    }
    if (this.m_workbook != null)
    {
      Marshal.ReleaseComObject((object) this.m_workbook);
      this.m_workbook = (Workbook) null;
    }
    if (this.m_excelApp != null)
    {
      Marshal.ReleaseComObject((object) this.m_excelApp);
      this.m_excelApp = (Application) null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    this.ResetThreadCultureInfo();
  }

  internal virtual void Quit()
  {
    if (this.m_excelApp != null)
    {
      if (this.m_sheet != null)
      {
        Marshal.ReleaseComObject((object) this.m_sheet);
        this.m_sheet = (Worksheet) null;
      }
      if (this.m_workbook != null)
      {
        // ISSUE: reference to a compiler-generated method
        this.m_workbook.Close((object) false, (object) Missing.Value, (object) Missing.Value);
        Marshal.ReleaseComObject((object) this.m_workbook);
        this.m_workbook = (Workbook) null;
      }
      // ISSUE: reference to a compiler-generated method
      this.m_excelApp.Workbooks.Close();
      // ISSUE: reference to a compiler-generated method
      this.m_excelApp.Quit();
      Marshal.ReleaseComObject((object) this.m_excelApp);
      this.m_excelApp = (Application) null;
      GC.Collect();
      GC.WaitForPendingFinalizers();
      GC.Collect();
    }
    this.ResetThreadCultureInfo();
  }

  protected void ResetThreadCultureInfo()
  {
    if (!this.m_cultureInfoChanged)
      return;
    Thread.CurrentThread.CurrentCulture = this.m_oldCultureInfo;
    this.m_cultureInfoChanged = false;
  }

  protected void SetThreadCultureInfo(CultureInfo newCultureInfo)
  {
    if (!this.m_cultureInfoChanged)
    {
      this.m_oldCultureInfo = Thread.CurrentThread.CurrentCulture;
      this.m_cultureInfoChanged = true;
    }
    Thread.CurrentThread.CurrentCulture = newCultureInfo;
  }

  protected Range GetRange(int firstRow, int firstCol, int lastRow, int lastCol)
  {
    // ISSUE: variable of a compiler-generated type
    Range range = (Range) null;
    if (firstRow >= 1 && firstCol >= 1 && lastRow >= firstRow && lastCol >= firstCol)
    {
      if (firstRow == lastRow && firstCol == lastCol)
      {
        range = this.m_sheet.Cells[(object) firstRow, (object) firstCol] as Range;
      }
      else
      {
        string cell1 = this.ConvertToCell(firstRow, firstCol);
        string cell2 = this.ConvertToCell(lastRow, lastCol);
        if (cell1 != null && cell2 != null)
        {
          // ISSUE: reference to a compiler-generated method
          range = this.m_sheet.get_Range((object) cell1, (object) cell2);
        }
      }
    }
    return range;
  }

  protected bool SetCellValue(object[,] values, int row, int column, object value)
  {
    bool flag = false;
    if (value != null && values != null && row >= 0 && row < values.GetLength(0) && column >= 0 && column < values.GetLength(1))
    {
      values[row, column] = value;
      flag = true;
    }
    return flag;
  }

  protected object GetCellValue(int row, int column)
  {
    object cellValue = (object) null;
    if (this.m_sheet != null)
    {
      if (this.m_sheet.Cells[(object) row, (object) column] is Range cell)
        cellValue = cell.Value2;
      Marshal.ReleaseComObject((object) cell);
    }
    return cellValue;
  }

  protected int ExtractFirstPositiveInt(string data)
  {
    int firstPositiveInt = -1;
    if (data != null)
    {
      string s = "";
      bool flag = false;
      for (int index = 0; index < data.Length; ++index)
      {
        if (data[index] >= '0' && data[index] <= '9')
        {
          s += data[index].ToString();
          flag = true;
        }
        else if (flag)
          break;
      }
      if (flag)
      {
        try
        {
          firstPositiveInt = int.Parse(s);
        }
        catch (Exception ex)
        {
          firstPositiveInt = -1;
        }
      }
    }
    return firstPositiveInt;
  }

  protected string ConvertToCell(int row, int column)
  {
    string cell = "";
    if (row > 0 && column > 0)
    {
      int num1 = column % 26;
      string str = cell + ((char) (65 + num1 - 1)).ToString();
      int num2 = (column - num1) / 26 % 26;
      if (num2 > 0)
        str = ((char) (65 + num2 - 1)).ToString() + str;
      cell = str + row.ToString();
    }
    return cell;
  }
}
